| With the advent of Java EE 5, Enterprise Beans can act as Web-Service components and since Java EE 6 they may be packaged within a WAR module as Java programming language class files. Java EE based web services come in two flavours: JAX-WS and JAX-RS. This tutorial examines JAX-WS and EJB 3.1. The Java EE 6 specifications include JAX-WS as one of its technologies. JAX-WS is the standard way to develop SOAP (Simple Object Access Protocol) web services in the Java platform. The JAX-WS Reference Implementation, included in GlassFish distributions, is Metro, a Web Services framework that provides tools and infrastructure to develop Web Services solutions. This tutorial demonstrates how to create a JAX-WS application based on stateless session EJBs with GlassFish Server and Eclipse for Java EE developers. A sample application is provided too.
Downloading, Installing, and Configuring Required SoftwareBefore you begin, download, install, and configure the required software.
Cannot resolve external resource into attachment. Setting Up the Database and Data SourceExactly how to set up the database and data source depends on the database that your application uses. For detailed instructions, see Administering Database Connectivity in Oracle GlassFish Server Administration Guide. Modifying the DerbyPool Connection PoolThe sample application uses the preconfigured DerbyPool connection pool for connections to the database. To enable the sample application to use this pool, you must modify the preset values of some properties of the pool. Before you begin, ensure that the option to start the Java DB process with GlassFish Server is set in the global preferences for the GlassFish Server.
On the JDBC Resources page, select jdbc/__default. DerbyPool is associated with this JNDI name as shown in the following figure. Cannot resolve external resource into attachment. Sample Application overviewThe sample application consists of two parts: a "server" application (web service provider) and a "client" application (web service consumer).
You can read more about the JAX-WS 2.2 standard at jax-ws.java.net , about the SOAP standard at www.w3.org, about the JPA standard at Java EE6 tutorial. To Convert the Sample Application to an Eclipse IDE ProjectThe sample application is distributed as a two web application archive (WAR) files. To use this applications, you must convert them to Eclipse IDE projects.
The server applicationAs of new EE6 specifications, stateless, stateful and singleton session beans can be used as web service provider. An EJB no longer requires a local business interface: it may expose a no-interface view. EJB's packaging has changed too. Enterprise beans, often providing the business logic of a web application or in this case of a web service, may be packaged within a WAR module as Java programming language class files or within a JAR file that is bundled within the WAR module.
To Run the Server Application
You don't need to run the command wsgen. From JAX-WS RI 2.1.4 onwards the runtime takes care of it by generating these classes and the WSDL (available at "http://localhost:8080/CustomerServiceBeanService/CustomerServiceBean?wsdl") file dynamically. The client application
@Stateless
@LocalBean
public class ClientEjbBean {
@WebServiceRef(wsdlLocation = "http://localhost.localdomain:8080/CustomerServiceBeanService/CustomerServiceBean?wsdl")
private CustomerServiceBeanService service;
public ClientEjbBean() {
// TODO Auto-generated constructor stub
}
public String invokeWS(Customer customer) throws InvalidCustomerException_Exception {
//business method
// it adds a business code to the customer
customer.setAppCode(new Long(1234567));
return this.addCustomer(customer);
}
private String addCustomer(ee.endpoint.Customer arg0) throws InvalidCustomerException_Exception {
ee.endpoint.CustomerServiceBean port = service.getCustomerServiceBeanPort();
return port.addCustomer(arg0);
}
}
@WebFault(name = "InvalidCustomerException", targetNamespace = "http://endpoint.ee/")
public class InvalidCustomerException_Exception
extends Exception
{
/**
* Java type that goes as soapenv:Fault detail element.
*
*/
private InvalidCustomerException faultInfo;
/**
*
* @param message
* @param faultInfo
*/
public InvalidCustomerException_Exception(String message, InvalidCustomerException faultInfo) {
super(message);
this.faultInfo = faultInfo;
}
/**
*
* @param message
* @param faultInfo
* @param cause
*/
public InvalidCustomerException_Exception(String message, InvalidCustomerException faultInfo, Throwable cause) {
super(message, cause);
this.faultInfo = faultInfo;
}
/**
*
* @return
* returns fault bean: ee.endpoint.InvalidCustomerException
*/
public InvalidCustomerException getFaultInfo() {
return faultInfo;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "customer", propOrder = {
"appCode",
"firstName",
"id",
"lastName"
})
public class Customer {
protected Long appCode;
protected String firstName;
protected Long id;
protected String lastName;
/**
* Gets the value of the appCode property.
*
* @return
* possible object is
* {@link Long }
*
*/
public Long getAppCode() {
return appCode;
}
/**
* Sets the value of the appCode property.
*
* @param value
* allowed object is
* {@link Long }
*
*/
public void setAppCode(Long value) {
this.appCode = value;
}
/**
* Gets the value of the firstName property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the value of the firstName property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setFirstName(String value) {
this.firstName = value;
}
/**
* Gets the value of the id property.
*
* @return
* possible object is
* {@link Long }
*
*/
public Long getId() {
return id;
}
/**
* Sets the value of the id property.
*
* @param value
* allowed object is
* {@link Long }
*
*/
public void setId(Long value) {
this.id = value;
}
/**
* Gets the value of the lastName property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getLastName() {
return lastName;
}
/**
* Sets the value of the lastName property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setLastName(String value) {
this.lastName = value;
}
}
For more information about the technologies in this tutorial, see the following documentation: |